package cn.com.tiptop.util.log;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 日志工具
 * 
 * @author qing
 *
 */
public class Logger {

	private static SimpleDateFormat sdf;
	private static String ineFeedCode;

	private Log log = null;
	
	static {
		try {
			String osname = "os.name";
			String windows = "windows";
			if (System.getProperty(osname).toLowerCase().startsWith(windows)) {
				ineFeedCode = "\r\n";
			} else {
				ineFeedCode = "\n";
			}
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// 装载log4j配置文件
			Log4jConfig.reload();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Logger() {
		try {
			log = LogFactory.getLog(this.getClass());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Logger(Class<?> c) {
		try {
			log = LogFactory.getLog(c);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Logger(String className) {
		try {
			log = LogFactory.getLog(className);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Logger getLogger() {
		try {
			return new Logger();
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Logger getLogger(Class<?> c) {
		try {
			return new Logger(c);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Logger getLogger(String className) {
		try {
			return new Logger(className);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public void trace(String info) {
		try {
			if (log.isTraceEnabled()) {
				log.trace(sdf.format(new Date()) + ineFeedCode + info);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void debug(String info) {
		try {
			if (log.isDebugEnabled()) {
				log.debug(sdf.format(new Date()) + ineFeedCode + info);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void info(String info) {
		try {
			if (log.isInfoEnabled()) {
				log.info(sdf.format(new Date()) + ineFeedCode + info);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void warn(String info) {
		try {
			if (log.isWarnEnabled()) {
				log.warn(sdf.format(new Date()) + ineFeedCode + info);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void error(String info) {
		try {
			if (log.isErrorEnabled()) {
				log.error(sdf.format(new Date()) + ineFeedCode + info);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void exception(Exception exception) {
		try {
			if (log.isErrorEnabled()) {
				log.error(sdf.format(new Date()) + ineFeedCode + exceptionToString(exception));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void fatal(String info) {
		try {
			if (log.isFatalEnabled()) {
				log.fatal(sdf.format(new Date()) + ineFeedCode + info);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean isTraceEnabled() {
		try {
			return log.isTraceEnabled();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isDebugEnabled() {
		try {
			return log.isDebugEnabled();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isInfoEnabled() {
		try {
			return log.isInfoEnabled();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isWarnEnabled() {
		try {
			return log.isWarnEnabled();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isErrorEnabled() {
		try {
			return log.isErrorEnabled();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean isFatalEnabled() {
		try {
			return log.isFatalEnabled();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public String exceptionToString(Exception exception) {
		StringWriter sw = null;
		PrintWriter pw = null;
		try {
			sw = new StringWriter();
			pw = new PrintWriter(sw);
			exception.printStackTrace(pw);
			String es = ineFeedCode + sw.toString() + ineFeedCode;
			if (pw != null) {
				pw.close();
			}
			if (sw != null) {
				sw.close();
			}
			return es;
		} catch (Exception e) {
			if (pw != null) {
				pw.close();
			}
			if (sw != null) {
				try {
					sw.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return "ErrorInfoFromException";
		} 
	}
}